python 读取多个含嵌套的json文件 并统计其中关键字的数量

您所在的位置:网站首页 python 数组个数 python 读取多个含嵌套的json文件 并统计其中关键字的数量

python 读取多个含嵌套的json文件 并统计其中关键字的数量

2023-09-29 07:10| 来源: 网络整理| 查看: 265

一.读取多个json文件

读取一个文件夹下的多个json文件 利用os库的listdir遍历文件夹,再用for循环可以load每个json文件,得到的json_data为dict类型

注意:for循环中使用open读取每个json文件,需要文件夹的路径也添加进去。

import os import json file_list = os.listdir(r"./车库0.3 json") print(file_list) for filename in file_list: with open(r"./车库0.3 json/"+filename, 'r') as load_f: json_data = json.load(load_f) # json files to dict:json_data print("file:", filename) 二.读取嵌套

参考的是大佬的文章,利用递归的思想,在关键字中再寻找关键字。

参考文章:无限遍历,Python实现在多维嵌套字典、列表、元组的JSON中获取数据

具体思路如下: 新建两个函数A和B。 函数 A处理字典数据,被调用后,判断传递的参数,如果参数为字典,则调用自身; 如果是列表或者元组,则调用列表处理函数B; 函数 B处理列表,被调用后,判断传递的参数,如果参数为列表或者元组,则调用自身; 如果是字典,则调用字典处理函数A。

代码中的get_target_value函数为A函数,_get_value为B函数。

# 处理嵌套json文件中指定关键字 # 处理字典值 def get_target_value(key, dic, tmp_list): """ :param key: 目标key值 :param dic: JSON数据 :param tmp_list: 用于存储获取的数据 :return: list """ if not isinstance(dic, dict) or not isinstance(tmp_list, list): # 对传入数据进行格式校验 return 'argv[1] not an dict or argv[-1] not an list ' if key in dic.keys(): tmp_list.append(dic[key]) # 传入数据存在则存入tmp_list for value in dic.values(): # 传入数据不符合则对其value值进行遍历 if isinstance(value, dict): get_target_value(key, value, tmp_list) # 传入数据的value值是字典,则直接调用自身 elif isinstance(value, (list, tuple)): _get_value(key, value, tmp_list) # 传入数据的value值是列表或者元组,则调用_get_value return tmp_list # 处理元组或列表值 def _get_value(key, val, tmp_list): for val_ in val: if isinstance(val_, dict): get_target_value(key, val_, tmp_list) # 传入数据的value值是字典,则调用get_target_value elif isinstance(val_, (list, tuple)): _get_value(key, val_, tmp_list) 三.整个程序

利用递归获取到json文件中的指定内容后,我需要做的是统计该关键字在本json文件中一共出现的数量。

思路:由于在上述递归过程中,得到的指定关键字对应的内容我们是用矩阵来存储的,于是,我们可以使用统计函数len()来统计其内容的个数,则是对应的关键字在json文件中出现的次数。

关键代码为:

number_count = len(list_shape)

整个程序的代码为:

import json import os # 处理嵌套json文件中指定关键字 # 处理字典值 def get_target_value(key, dic, tmp_list): """ :param key: 目标key值 :param dic: JSON数据 :param tmp_list: 用于存储获取的数据 :return: list """ if not isinstance(dic, dict) or not isinstance(tmp_list, list): # 对传入数据进行格式校验 return 'argv[1] not an dict or argv[-1] not an list ' if key in dic.keys(): tmp_list.append(dic[key]) # 传入数据存在则存入tmp_list for value in dic.values(): # 传入数据不符合则对其value值进行遍历 if isinstance(value, dict): get_target_value(key, value, tmp_list) # 传入数据的value值是字典,则直接调用自身 elif isinstance(value, (list, tuple)): _get_value(key, value, tmp_list) # 传入数据的value值是列表或者元组,则调用_get_value return tmp_list # 处理元组或列表值 def _get_value(key, val, tmp_list): for val_ in val: if isinstance(val_, dict): get_target_value(key, val_, tmp_list) # 传入数据的value值是字典,则调用get_target_value elif isinstance(val_, (list, tuple)): _get_value(key, val_, tmp_list) # 处理单个json文件 # 修改json文件所在路径 def single_file_main(): path = "json_files/self_val_person_day.json" with open(path, 'r') as load_f: json_data = json.load(load_f) # json files to dict:json_data number_shape = len(json_data.keys()) # json.key(): get all the images of one file(get number of images) # data = list(json_data.keys()) # 可以索引 print(type(json_data)) print("number:", number_shape) list_shape = get_target_value('shape_attributes', json_data, []) number_count = len(list_shape) print("shape_attributes:", list_shape) print("numbers:", number_count) # 批量处理指定文件夹下多个json文件 # 修改json文件所在的文件夹路径 def main(): file_list = os.listdir(r"./json_files") print(file_list) for filename in file_list: with open(r"./json_files/"+filename, 'r') as load_f: json_data = json.load(load_f) # json files to dict:json_data list_shape = get_target_value('shape_attributes', json_data, []) # 利用json文件中的shape_attributes关键字可对应圈出的框的数量 number_count = len(list_shape) print("file:", filename) print("numbers:", number_count) if __name__ == '__main__': main()

得到的结果为:

/home/wenhaolun/anaconda3/bin/python /home/wenhaolun/PycharmProjects/pythonProject2/read_json.py ['三产(2).json', '三产(1).json', '四产(3).json', '四产(2).json', '车牌数据集-待标注.json', '三产(3).json', 'self_val_person_day.json', 'car,.json', '白天室外车牌.json', '四产(1).json', '同一车牌不同角度.json', 'self_val_person_night.json'] file: 三产(2).json numbers: 309 file: 三产(1).json numbers: 1178 file: 四产(3).json numbers: 647 file: 四产(2).json numbers: 1360


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3